//
//  WaterFlow.swift
//  SwitfTest
//
//  Created by runone02 on 2019/1/10.
//  Copyright © 2019年 liqiyun. All rights reserved.
//

import UIKit

class WaterFlow: UICollectionViewFlowLayout {
    lazy var dataArray: NSMutableArray = NSMutableArray()
    lazy var attrArray: NSMutableArray = NSMutableArray()
    var lie:Int = 0
    lazy var framYArray = [CGFloat](repeating: self.sectionInset.top, count: lie)
    
    override init() {
        super .init()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("错误")
    }
    
    
    /// CollectionView布局
    ///
    /// - Parameters:
    ///   - dataA: 数据
    ///   - coumns: 每行个数
    ///   - marginLeft: y左边间距
    ///   - marginRight: 右边间距
    ///   - magrinMinH: 最小高度
    ///   - marginMinW: 最小宽度
    func layout(dataA:NSMutableArray,coumns:Int,marginTop:CGFloat,marginLeft:CGFloat,marginbottom:CGFloat,marginRight:CGFloat,magrinMinH:CGFloat,marginMinW:CGFloat){
        self.dataArray = dataA
        self.minimumInteritemSpacing = magrinMinH
        self.minimumLineSpacing = marginMinW
        self.sectionInset = UIEdgeInsets(top: marginTop, left: marginLeft, bottom: marginbottom, right: marginRight)
        self.lie = coumns
    }
    
    override func prepare() {
        for i in 0..<self.dataArray.count {
            let indexpath = IndexPath(item: i, section: 0)
            let attrs = UICollectionViewLayoutAttributes(forCellWith: indexpath)
            
            //计算每个高度
            let allwidth = (self.collectionView?.bounds.size.width)!
            let marginleft = self.sectionInset.left
            let marginRight = self.sectionInset.right
            let totalMargin:CGFloat = ((CGFloat)(lie - 1)*self.minimumLineSpacing)
            let itemw:CGFloat  = (allwidth - marginRight - marginleft - totalMargin) / (CGFloat)(lie)
            
            //计算每个高度
            let itemh = 80 * itemw / 80
            
            //计算每个X
            //计算当前cell使第几列
            let colunm = self.getMinYoftheframeY(frameYArray: self.framYArray)
            let itemX = marginleft + (self.minimumLineSpacing + itemw)*(CGFloat)(colunm)
            
            let itemY = self.framYArray[colunm]
            attrs.frame = CGRect(x: itemX, y: itemY, width: itemw, height: itemh)
            self.framYArray[colunm] = self.framYArray[colunm]+itemh + self.minimumLineSpacing
            self.attrArray.add(attrs)
        }
    }
    
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        
        return self.attrArray as? [UICollectionViewLayoutAttributes]
    }
    
    override var collectionViewContentSize: CGSize{
        //假设数组的第一列值最大
        var maxY = self.framYArray[0];
        for i in 0 ..< self.framYArray.count {
            if(maxY<self.framYArray[i]){
                maxY = self.framYArray[i]
            }
        }
  
        return CGSize(width: (self.collectionView?.bounds.size.width)!, height: maxY)
    }
    
    

    func getMinYoftheframeY(frameYArray:[CGFloat]) -> (Int) {
        //假设第0列的最大Y值最小
        var minIndex:Int = 0
        var minY = frameYArray[minIndex]
        for u in 0..<frameYArray.count {
            if(minY > frameYArray[u]){
            minY = frameYArray[u]
            minIndex = u
        }
        }
        return minIndex
    }
    
}
